package sample.stormax.pres.news.fragments;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import sample.stormax.pres.news.R;
import sample.stormax.pres.news.adapters.NewsAdapter;
import sample.stormax.pres.news.adapters.PagerAdapter;
import sample.stormax.pres.news.utils.sqlite.FavouriteSQLiteImpl;
import sample.stormax.pres.news.utils.sqlite.ISQLiteOperation;

/**
 * NewsFragment的容器
 */
public class NewsContainerFragment extends Fragment {

    private static final String TAG = "NewsContainerFragment";
    //bundle key while create a newInstance
    private static final String TYPE = "type";
    private static final String TYPE_CN = "typeCN";

    //新闻类别顺序表
    private ArrayList<String> types;
    private ArrayList<String> typesCN;

    //当前Fragment单例
    private static NewsContainerFragment containerFragment;
    //需要填充的newsFragment
    private static NewsFragment[] fragments;

    //是否重新适配Fragment
    private boolean reLoadFragment = false;

    //数据库操作类
    private static ISQLiteOperation favouriteOp;

    @BindView(R.id.news_pager)
    ViewPager newsPager;
    @BindView(R.id.news_tab)
    TabLayout newsTab;

    //本类的视图
    private View view;
    private FragmentManager fragmentManager;
    private FragmentStatePagerAdapter pagerAdapter;

    public NewsContainerFragment() {
    }

    public static NewsContainerFragment newInstance(ArrayList<String> types, ArrayList<String> typesCN) {
        containerFragment = containerFragment == null ? new NewsContainerFragment() : containerFragment;
        Bundle args = new Bundle();
        args.putStringArrayList(TYPE, types);
        args.putStringArrayList(TYPE_CN, typesCN);
        containerFragment.setArguments(args);
        fragments = new NewsFragment[types.size()];
        return containerFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建数据库操作类
        favouriteOp = FavouriteSQLiteImpl.newInstance(getContext());
        favouriteOp.createTable();

        if (getArguments() != null) {
            types = getArguments().getStringArrayList(TYPE);
            typesCN = getArguments().getStringArrayList(TYPE_CN);
        }

        //初始化要加载的fragment
        if (fragments[0] == null) {
            Log.d(TAG, "waste time");
            for (int i = 0; i < fragments.length; i++) {
                fragments[i] = NewsFragment.newInstance(types.get(i));
            }
            //新fragment需要重新适配
            reLoadFragment = true;
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Log.d(TAG, "onCreateView");
        if (view == null)
            view = inflater.inflate(R.layout.fragment_news_contrainer, container, false);
        ButterKnife.bind(this, view);

        fragmentManager = getActivity().getSupportFragmentManager();

        //设置viewpager的适配器
        setAdapter();

        //设置tabLayout的填充方式
        if (types.size() <= 5)
            newsTab.setTabMode(TabLayout.MODE_FIXED);
        else
            newsTab.setTabMode(TabLayout.MODE_SCROLLABLE);

        //tabLayout与pagerView建立监听
        newsPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(newsTab));
        return view;
    }

    //fragmentManager中已经存在fragment，不需要重新设置适配器
    private void setAdapter() {
        if (pagerAdapter == null || reLoadFragment) {
            Log.d(TAG, "new Pager Adapter");
            pagerAdapter = new PagerAdapter(fragmentManager, fragments, types, typesCN);
            newsPager.setAdapter(pagerAdapter);
            reLoadFragment = false;
        } else {
            Log.d(TAG, "reuse adapter");
        }
    }
}
